﻿using EislaufScb.Model.Repository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace EislaufScb.Model.Test
{   
    
    /// <summary>
    ///This is a test class for RepositoryDbContextTest and is intended
    ///to contain all RepositoryDbContextTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BaseRepositoryTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyDbRepositoryTestInitialize(TestContext testContext)
        {
            ClubModelContainer ctx = new ClubModelContainer();

            //using (ctx)
            //{
            //    if (ctx.DatabaseExists())
            //    {
            //        ctx.DeleteDatabase();
            //    }
                
            //    ctx.CreateDatabase();
                
            //}
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyDbRepositoryTestCleanup()
        {
            
        }
        //
        #endregion


        /// <summary>
        ///A test for RepositoryDbContext Constructor
        ///</summary>
        [TestMethod()]
        public void RepositoryDbContextConstructorTest()
        {
            BaseRepository target = new BaseRepository(new ClubModelContainer());
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for CreatePersistenceObject
        ///</summary>
        [TestMethod()]
        public void CreatePersistenceObjectTestHelper()
        {
            Club club = new Club() { Name = "MusterClub", FoundingYear = new DateTime(1977, 1, 7), Url = "www.musterclub.ch", History = "Muster history", Type = "IceSkating" };

            BaseRepository target = new BaseRepository(new ClubModelContainer());
            target.AddEntry<Club>(club);
            target.Save(club);

            Assert.IsNotNull(target.GetEntryById<Club>(club.Id));

        }

        [TestMethod()]
        public void RemoveEntryByIdTest()
        {
            Club club = null;

            using (BaseRepository context = new BaseRepository(new ClubModelContainer()))
            {
                club = new Club() { Name = "MusterClub 01", FoundingYear = new DateTime(1977, 7, 7), Url = "www.musterclub.ch", History = "Muster history", Type = "IceSkating" };

                context.AddEntry<Club>(club);
                context.Save(club);
            }

            using (BaseRepository context = new BaseRepository(new ClubModelContainer()))
            {
                Club clb = context.GetObject<Club>(club);
                context.RemoveEntryById<Club>(club.Id);
                context.Save(club);

                Assert.IsNull(context.GetEntryById<Club>(club.Id));
            }
        }

        [TestMethod()]
        public void UpdateEntryByIdTest()
        {
            Club clubOriginal = null;
            Club clubUpdated = null;
            string myUpdatedName = "ich bin aktualisiert worden";
            long objectId = 0;

            using (BaseRepository context = new BaseRepository(new ClubModelContainer()))
            {
                clubOriginal = new Club() { Name = "MusterClub 02", FoundingYear = new DateTime(1977, 8, 7), Url = "www.musterclub.ch", History = "Muster history", Type = "IceSkating" };


                context.AddEntry<Club>(clubOriginal);
                context.Save(clubOriginal);

                objectId = clubOriginal.Id;
            }

            using (BaseRepository context = new BaseRepository(new ClubModelContainer()))
            {
                clubUpdated = context.GetObject<Club>(clubOriginal);
                clubUpdated.Name = myUpdatedName;

                Assert.IsTrue(clubUpdated.EntityState == System.Data.EntityState.Modified);

                context.UpdateEntry<Club>(clubUpdated);
                context.Save(clubOriginal);
            }

            using (ClubModelContainer context = new ClubModelContainer())
            {
                Assert.AreEqual(myUpdatedName, context.BaseEntitySet.OfType<Club>().Where(c => c.Id == objectId).First().Name);
            }
        }

        [TestMethod()]
        public void GetPersistenceObjectsTest()
        {
            using (BaseRepository context = new BaseRepository(new ClubModelContainer()))
            {
                Assert.IsTrue(context.GetAll<Club>().Count > 0);
            }
        }
    }
}
